package com.zone.vehicle.superapp.view.home.home.main.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.zone.sdk.module.appstore.entity.AppListItemEntity;
import com.zone.sdk.module.appstore.entity.ResourceItem;
import com.zone.vehicle.lib.view.utils.ImageUtil;
import com.zone.vehicle.sdk.module.superapp.vehicle.utils.NetUtil;
import com.zone.vehicle.superapp.R;
import com.zone.vehicle.superapp.utils.SharedPreferencesUtil;
import com.zone.vehicle.superapp.view.SuperApplication;
import com.zone.vehicle.superapp.view.home.home.main.AppHorizontalFragment.AppItemOnClickListener;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.song.videoplayer.ZoneQSVideoView;
import org.song.videoplayer.IVideoPlayer;
import org.song.videoplayer.PlayListener;
import org.song.videoplayer.QSVideo;
import org.song.videoplayer.media.AndroidMedia;
import org.song.videoplayer.media.IjkExoMedia;

import java.util.List;

import static com.zone.vehicle.superapp.view.common.base.Constants.MUTE;
import static org.song.videoplayer.IVideoPlayer.*;

public class HomeAppAdapterAdapter extends RecyclerView.Adapter<HomeAppAdapterAdapter.ViewHolder> {

    private static final String TAG = HomeAppAdapterAdapter.class.getSimpleName();

    public void setData(List<AppListItemEntity> data) {
        this.data = data;
    }

    private List<AppListItemEntity> data;

    private AppItemOnClickListener listener;
    private Activity activity;
    private int autoPlay = 1; //0:不自动播放 1:wifi下自动播放 2:自动播放
    private boolean mute = false; //默认静音
    private boolean isPlaying = false;

    public HomeAppAdapterAdapter(List<AppListItemEntity> data, AppItemOnClickListener listener, Activity activity) {
        this.data = data;
        this.listener = listener;
        this.activity = activity;
        mute = SharedPreferencesUtil.getInstance(activity).getBoolean(MUTE, false);
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fragment_home_app_item, parent, false);
        v.findViewById(R.id.item1).setOnClickListener(listener);
        v.findViewById(R.id.item2).setOnClickListener(listener);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
        if (ObjectUtils.isEmpty(data)) {
            return;
        }
        int index1 = position * 2;
        AppListItemEntity app1 = data.get(index1);
        if (ObjectUtils.isNotEmpty(app1)) {
            ResourceItem item = app1.getVideoItem();
            if (null != item && StringUtils.isNotEmpty(item.getResourceUrl())) {
                holder.pic1.setVisibility(View.GONE);
                holder.qs1.setVisibility(View.VISIBLE);
                if (null != app1.getPicItem() && StringUtils.isNotEmpty(app1.getPicItem().getResourceUrl())) {
                    ImageUtil.load(activity, holder.qs1.getCoverImageView(), app1.getPicItem().getResourceUrl());
                }
                //进入全屏的模式 0横屏 1竖屏 2传感器自动横竖屏 3根据视频比例自动确定横竖屏      -1什么都不做
                holder.qs1.enterFullMode = 0;
                holder.qs1.setMute(mute);
                //是否非全屏下也可以手势调节进度
//                holder.qs1.isWindowGesture = true;
                holder.qs1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        holder.qs1.play();
                    }
                });
                ResourceItem finalItem = item;
                holder.qs1.setPlayListener(new PlayListener() {
                    int mode;
                    @Override
                    public void onStatus(int status, String url) {//播放状态
                        if (status == IVideoPlayer.STATE_AUTO_COMPLETE && url.equals(finalItem.getResourceUrl())) {
                            holder.qs1.quitWindowFullscreen();//播放完成退出全屏
                            playNext(holder.qs1, index1);
                        }
                    }

                    @Override//全屏/普通/浮窗
                    public void onMode(int mode) {
                        this.mode = mode;
                    }

                    @Override
                    public void onEvent(int what, Integer... extra) {
                        if (what == EVENT_MUTE_CHANGED){
                            SharedPreferencesUtil.getInstance(activity).setBoolean(MUTE, extra[0]==0);
                        }else if (what == EVENT_PAUSE){
                            holder.qs1.pause();
                        }
                    }

                    @Override
                    public void onPlaying(String url) {
                        if (!url.equals(finalItem.getResourceUrl())){
                            holder.qs1.pause();
                        }
                    }

                    @Override
                    public void notifyPlaying(String url) {
                        if (url.equals(finalItem.getResourceUrl())){
                            holder.qs1.play();
                        }
                    }
                });
                holder.qs1.release();
//                holder.qs1.setDecodeMedia(AndroidMedia.class);
                holder.qs1.setUp(QSVideo.Build(item.getResourceUrl()).build());
                if (!isPlaying && ((autoPlay == 1 && SuperApplication.NET_STATUS == NetUtil.NETWORK_WIFI)
                        || autoPlay == 2)){
                    isPlaying = true;
                    holder.qs1.play();
                }
            } else {
                item = app1.getPicItem();
                if (null != item && StringUtils.isNotEmpty(item.getResourceUrl())) {
                    holder.pic1.setVisibility(View.VISIBLE);
                    ImageUtil.load(activity, holder.pic1, item.getResourceUrl());
                }
            }

            holder.itemView.findViewById(R.id.item1).setTag(app1);
            holder.name1.setText(app1.getSpuName());
            holder.score1.setText(app1.getProductScore());
            if (app1.getLabelList() != null) {
                String label = app1.getLabel(0);
                if (null != label) {
                    holder.tag11.setText(label);
                    holder.tag11.setVisibility(View.VISIBLE);
                }
                label = app1.getLabel(1);
                if (null != label) {
                    holder.tag12.setText(label);
                    holder.tag12.setVisibility(View.VISIBLE);
                }
                label = app1.getLabel(2);
                if (null != label) {
                    holder.tag13.setText(label);
                    holder.tag13.setVisibility(View.VISIBLE);
                }
            }
            holder.description1.setText(app1.getDescription());
        }
        int index2 = position * 2 + 1;
        if (data.size() <= index2) {
            holder.itemView.findViewById(R.id.item2).setVisibility(View.INVISIBLE);
            return;
        }
        AppListItemEntity app2 = data.get(index2);
        if (ObjectUtils.isNotEmpty(app2)) {
            ResourceItem item = app2.getVideoItem();
            if (null != item && StringUtils.isNotEmpty(item.getResourceUrl())) {
                holder.pic2.setVisibility(View.GONE);
                holder.qs2.setVisibility(View.VISIBLE);
                if (null != app1.getPicItem() && StringUtils.isNotEmpty(app1.getPicItem().getResourceUrl())) {
                    ImageUtil.load(activity, holder.qs2.getCoverImageView(), app1.getPicItem().getResourceUrl());
                }
                //进入全屏的模式 0横屏 1竖屏 2传感器自动横竖屏 3根据视频比例自动确定横竖屏      -1什么都不做
                holder.qs2.enterFullMode = 0;
                //是否非全屏下也可以手势调节进度
//                holder.qs2.isWindowGesture = true;
                holder.qs2.setMute(mute);
                holder.qs2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        holder.qs2.play();
                    }
                });
                ResourceItem finalItem = item;
                holder.qs2.setPlayListener(new PlayListener() {
                    int mode;
                    @Override
                    public void onStatus(int status, String url) {//播放状态
                        if (status == IVideoPlayer.STATE_AUTO_COMPLETE && url.equals(finalItem.getResourceUrl())) {
                            holder.qs2.quitWindowFullscreen();//播放完成退出全屏
                            playNext(holder.qs2, index2);
                        }
                    }

                    @Override//全屏/普通/浮窗
                    public void onMode(int mode) {
                        this.mode = mode;
                    }

                    @Override
                    public void onEvent(int what, Integer... extra) {
                        if (what == EVENT_MUTE_CHANGED){
                            SharedPreferencesUtil.getInstance(activity).setBoolean(MUTE, extra[0]==0);
                        }else if (what == EVENT_PAUSE){
                            holder.qs2.pause();
                        }
                    }

                    @Override
                    public void onPlaying(String url) {
                        if (!url.equals(finalItem.getResourceUrl())){
                            holder.qs2.pause();
                        }
                    }

                    @Override
                    public void notifyPlaying(String url) {
                        if (url.equals(finalItem.getResourceUrl())){
                            holder.qs2.play();
                        }
                    }
                });
                holder.qs2.release();
                holder.qs2.setUp(QSVideo.Build(item.getResourceUrl()).build());
                if (!isPlaying && ((autoPlay == 1 && SuperApplication.NET_STATUS == NetUtil.NETWORK_WIFI)
                        || autoPlay == 2)){
                    holder.qs2.play();
                }
            } else {
                item = app2.getPicItem();
                if (null != item && StringUtils.isNotEmpty(item.getResourceUrl())) {
                    holder.pic2.setVisibility(View.VISIBLE);
                    ImageUtil.load(activity, holder.pic2, item.getResourceUrl());
                }
            }
            holder.itemView.findViewById(R.id.item2).setTag(app2);
            holder.name2.setText(app2.getSpuName());
            holder.score2.setText(app2.getProductScore());
            if (app2.getLabelList().size() > 0) {
                String label = app2.getLabel(0);
                if (null != label) {
                    holder.tag21.setText(label);
                    holder.tag21.setVisibility(View.VISIBLE);
                }
                label = app2.getLabel(1);
                if (null != label) {
                    holder.tag22.setText(label);
                    holder.tag22.setVisibility(View.VISIBLE);
                }
                label = app2.getLabel(2);
                if (null != label) {
                    holder.tag23.setText(label);
                    holder.tag23.setVisibility(View.VISIBLE);
                }
            }
            holder.description2.setText(app2.getDescription());
        }
    }

    public void playNext(ZoneQSVideoView qs, int index) {
        if ((autoPlay == 1 && SuperApplication.NET_STATUS != NetUtil.NETWORK_WIFI)
                || autoPlay == 0){
            return;
        }
        for (int i = index+1; i < data.size(); i++) {
            AppListItemEntity appEntity = data.get(i);
            if (appEntity.getVideoItem() != null) {
                qs.notifyPlay(appEntity.getVideoItem().getResourceUrl());
                return;
            }
        }
        playNext(qs, 0);
    }

    @Override
    public int getItemCount() {
        if (ObjectUtils.isNotEmpty(data)) {
            if (data.size() % 2 == 1) {
                return (data.size() / 2) + 1;
            } else {
                return data.size() / 2;
            }
        } else {
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView pic1;
        private final TextView name1;
        private final ImageView star1;
        private final TextView score1;
        private final TextView tag11;
        private final TextView tag12;
        private final TextView tag13;
        private final TextView description1;
        private final ZoneQSVideoView qs1;

        private final ImageView pic2;
        private final TextView name2;
        private final ImageView star2;
        private final TextView score2;
        private final TextView tag21;
        private final TextView tag22;
        private final TextView tag23;
        private final TextView description2;
        private final ZoneQSVideoView qs2;


        public ViewHolder(View itemView) {
            super(itemView);
            pic1 = itemView.findViewById(R.id.pic1);
            name1 = itemView.findViewById(R.id.name1);
            star1 = itemView.findViewById(R.id.star1);
            score1 = itemView.findViewById(R.id.score1);
            tag11 = itemView.findViewById(R.id.tag11);
            tag12 = itemView.findViewById(R.id.tag12);
            tag13 = itemView.findViewById(R.id.tag13);
            description1 = itemView.findViewById(R.id.description1);
            qs1 = itemView.findViewById(R.id.qs1);

            pic2 = itemView.findViewById(R.id.pic2);
            name2 = itemView.findViewById(R.id.name2);
            star2 = itemView.findViewById(R.id.star2);
            score2 = itemView.findViewById(R.id.score2);
            tag21 = itemView.findViewById(R.id.tag21);
            tag22 = itemView.findViewById(R.id.tag22);
            tag23 = itemView.findViewById(R.id.tag23);
            description2 = itemView.findViewById(R.id.description2);
            qs2 = itemView.findViewById(R.id.qs2);
        }
    }


}
